# 生成测试集
# 用于对单个类的正确率做记录
# 所以此方法把测试集中的每个类单独生成一个测试文件夹
# 此方法针对外来入侵使用，其他方法不适用
import os
import shutil

img_paths = "G:\学习\数据集\实验室数据集原版\外来入侵"
save_path = "G:\学习\数据集\实验室数据集操作版\外来入侵零散测试集生成"
filelist = os.listdir(img_paths)
for mode in filelist:
    if mode == "test":
        folders = os.listdir(os.path.join(img_paths, mode))
        for folder in folders:
            if not os.path.exists(os.path.join(save_path, "test" + str(folder))):
                os.mkdir(os.path.join(save_path, "test" + str(folder)))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "train"))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "test"))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "val"))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "train", folder))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "test", folder))
                os.mkdir(os.path.join(save_path, "test" + str(folder), "val", folder))
            imgs = os.listdir(os.path.join(img_paths, mode, folder))
            print(folder)
            for img in imgs:
                shutil.copy(os.path.join(img_paths, mode, folder, img),
                            os.path.join(save_path, "test" + str(folder), "test", str(folder), img))
    if mode == "train":
        folders = os.listdir(os.path.join(img_paths, mode))
        for folder in folders:

            imgs = os.listdir(os.path.join(img_paths, mode, folder))
            print(folder)
            for img in imgs:
                shutil.copy(os.path.join(img_paths, mode, folder, img),
                            os.path.join(save_path, "test" + str(folder), "train", str(folder), img))
    if mode == "val":
        folders = os.listdir(os.path.join(img_paths, mode))
        for folder in folders:

            imgs = os.listdir(os.path.join(img_paths, mode, folder))
            print(folder)
            for img in imgs:
                shutil.copy(os.path.join(img_paths, mode, folder, img),
                            os.path.join(save_path, "test" + str(folder), "val", str(folder), img))
